using System;
using System.Collections;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Web.Configuration;

using ObjectRelationalMapping.Helper;
using WebConfiguration = System.Configuration.Configuration;

namespace ObjectRelationalMapping.Configuration
{
    internal class Handler
    {
        public static void GetSettings ( )
        {
            if ( Directory.Exists ( Common.ProjectDirectory )
                && File.Exists ( Common.ProjectDirectory + @"web.config" ) )
            {
                ExeConfigurationFileMap configurationFileMap =
                    new ExeConfigurationFileMap ();
                configurationFileMap.ExeConfigFilename =
                    Common.ProjectDirectory + @"web.config";
                WebConfiguration webConfiguration =
                    ConfigurationManager.OpenMappedExeConfiguration (
                        configurationFileMap, ConfigurationUserLevel.None );
                ConfigurationSection configurationSection =
                    webConfiguration.Sections[ 
                        Common.Constants.CONFIGURATION_SECTION ];
                if ( configurationSection != null )
                {
                    MappingSection mappingSection =
                        ( MappingSection )configurationSection;
                    Common.ApplicationNamespace =
                        mappingSection.ApplicationNamespace;
                    Common.DatabaseConnectionString =
                        mappingSection.DatabaseConnectionString;

                    Console.WriteLine (
                        "Application Namespace: {0}",
                        Common.ApplicationNamespace );
                    Console.WriteLine ( 
                        "Database Connection String: {0}", 
                        Common.DatabaseConnectionString );
                }
            }
            else
            {
                throw new Exception ( 
                    "Error: The configuration file could not be found." );
            }
        }
    }
}